/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hanserver.dao;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Dao implements Serializable {

    private static final long serialVersionUID = 1L;
    public static String status = "Não conectou...";

    public Dao() {
    }

    public void intert(String note) throws SQLException {
        try {
            Statement st = getConn().createStatement();

            int rturn = st.executeUpdate("INSERT INTO `tabela_teste`(`nr_sequencia`, `ds_campo`) VALUES (0,'" + note + "')");
            Logger.getLogger(Dao.class.getSimpleName()).log(Level.SEVERE, "Reuturn insert -->" + rturn);
            System.out.println("Reuturn insert -->" + rturn);
        } finally {
            closeConn();
        }
    }

    public static java.sql.Connection getConn() {
        Connection connection = null;
        try {
            String driverName = "com.mysql.jdbc.Driver";
            Class.forName(driverName);
            String serverName = "";
            try {
                if ("v-VAIO".equals(getServerName())
                        || "Karine".equals(getServerName())) {
                    serverName = "ftp.chapolinekarine.com.br:3306";
                } else {
                    serverName = "localhost";
                }
            } catch (Exception ex) {
                Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
            }

            String mydatabase = "chapoli_sales";
            String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
            String username = "chapoli_sales";
//            String username = "root";
            String password = "67958998*//*";
            connection = DriverManager.getConnection(url, username, password);
            if (connection != null) {
                status = ("STATUS--->Conectado com sucesso!");
            } else {
                status = ("STATUS--->Não foi possivel realizar conexão ");
            }
            return connection;
        } catch (ClassNotFoundException e) {
            System.out.println("O driver expecificado nao foi encontrado." + e);
            return null;
        } catch (SQLException e) {
            System.out.println("Nao foi possivel conectar ao Banco de Dados." + e);
            return null;
        }
    }

    public static String getServerName() throws UnknownHostException {
        return InetAddress.getLocalHost().getHostName();
    }

    public static String statusConection() {
        return status;
    }

    public static boolean closeConn() {
        try {
            Dao.getConn().close();
            return true;
        } catch (SQLException e) {
            return false;
        }
    }

    public static java.sql.Connection restartConn() {
        closeConn();
        return Dao.getConn();
    }
}
